Beaver.start = function()
{
	Ext.QuickTips.init();
	
	Beaver.Application = new Ext.App({});
	
	Ext.data.DataProxy.addListener('write', function(proxy, action, result, res, rs) {
    	if ( res.result.message ) Beaver.Application.setAlert(true, action + ':' + res.result.message);
	});
	
	Beaver.Loader.addModuleToQueue("general");
	Beaver.Loader.addModuleToQueue("user");
	Beaver.Loader.start( function(){
		
		Beaver.Modules.User.onLoginSuccess = function()
		{
			Beaver.Modules.User.hideLoginScreen();
			if( !Beaver.applicationStarted )
			{
				Beaver.startApplication();
			}
			else if ( null != Beaver.Modules.Layout.updateStatusbar )
			{
				Beaver.Modules.Layout.updateStatusbar();
			}
		}
		
		Beaver.sessionCheckLoop();
	});
};

Beaver.sessionCheckLoop = function()	
{
	// add overtime to session lifetime
	var lifetime = Beaver.Modules.User.sessionLifetime * 1000 + 5000;
	console.log(lifetime/1000);
	// add pooing provider to check the session
	Ext.Direct.addProvider({
        type:'polling',
        url: 'module/user/checksession',
		interval: lifetime
	});

	// event to check the session
	Ext.Direct.on('checksession', function( event )
	{
		if ( event.data == false ) {
			Beaver.Modules.User.showLoginScreen();
		} else {
			Beaver.Modules.User.data = event.data;
			Beaver.Modules.User.hideLoginScreen();
			if( !Beaver.applicationStarted ) Beaver.startApplication();
		}
	});
}

Beaver.startApplication = function()
{
	
	Beaver.applicationStarted = true;
	Beaver.Loader.addModuleToQueue("layout");
	Beaver.Loader.addModuleToQueue("country");
	Beaver.Loader.addModuleToQueue("city");
	Beaver.Loader.addModuleToQueue("partner");
	Beaver.Loader.start( function(){
		Beaver.Modules.Layout.show();
	});
}